
var _grid = $('#jgTable');
gridWidth = $('.u_toolbar');

$(function () {
    _gridInit();
    loadOption();
    $('li[data-opt="add"]').on('click', addCode);
    $('li[data-opt="edit"]').on('click', function(){
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        editCode(rowId);
    });
    $("li[data-opt='delete']").on('click', function(){
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        deleteCode(rowId);
    });
    $("li[data-opt='password']").on('click', function () {
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        setPwd(rowId);
    });
    $("li[data-opt='opt_search']").find(">i").on('click', function(){
        gridSearch.Content = {
            SearchText: $("#opt_search").find(">input").val()
        };
        search();
    });
    $('.modal-data').draggable();
});


function loadOption(){  //MDMCommonality01
    var _postData = {
        Content:{
            CodeList: ['MDM0002']
        }
    };
    for(i in postData){
        _postData[i] = postData[i];
    };
    var options = '';
    $.ajax({
        async: false,
        type: "post",
        url: mdmPath + "/MDMCommonality/MDMCommonality01",
        data: _postData,
        dataType: "json",
        success: function (re) {
            if (re.Status == 200) {
                var MDM0002 = re.Content.DropdownData.MDM0002;
                for (var i in MDM0002) {
                    options += '<option value="' + i + '">' + MDM0002[i] + '</option>';
                }
                $('div[data-opt="Drop_down_02"]').find('>select').html(options);
            } else {
                myAlert(re.Message);
            }
        }
    }); 
}

function addCode(){
    $('#m_Code').val('').attr('disabled', false);
    $('#m_Name').val('').parent().parent().removeClass('hidden');
    $('#m_Department').val('D000').parent().parent().removeClass('hidden');
    $('#m_Password').parent().parent().addClass('hidden');
    $("input[type='checkbox']").prop("checked", false);
    $('.checkbox').removeClass('hidden');
    $('.modal-overlay').removeClass('hidden');
    var _postData = Object.create(postData);
    _postData.Method = 'A';
    submit(_postData);
}
function editCode(rowId){
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        $('#m_Code').val(rowData.Code).attr('disabled', true);
        $('#m_Name').val(rowData.Name).parent().parent().removeClass('hidden');
        $('#m_Department').val(rowData.Department).parent().parent().removeClass('hidden');
        $('#m_Password').parent().parent().addClass('hidden');
        $('.checkbox').removeClass('hidden');
        if(rowData.SuperUser == 'Y'){
            $("input[type='checkbox']").prop("checked", true);
        }else if(rowData.SuperUser == 'N'){
            $("input[type='checkbox']").prop("checked", false);
        }
        $('.modal-overlay').removeClass('hidden');
        var _postData = Object.create(postData);
        _postData.Method = 'U';
        submit(_postData);
    }else{
        myAlert('请选择您要编辑的员工');
    }
}
function deleteCode(rowId){
    if(rowId){  //判断是否选中有效行
        //var rowData = _grid.jqGrid('getRowData', rowId);
        var _postData = Object.create(postData);
        _postData.Method = 'D';
        console.log(_postData)
    }else{
        myAlert('请选择您要删除的员工');
    } 
}
function submit(_postData){ //MDM000101
    $('#submitBtn').unbind('click').bind('click', function(){
        if($("input[type='checkbox']").is(':checked')){
            var SuperUser = 'Y';
        }else{
            var SuperUser = 'N';
        }
        _postData.Content = {
            Code: $('#m_Code').val(),
            Name: $('#m_Name').val(),
            Department: $('#m_Department').val(),
            SuperUser: SuperUser
        }
        console.log(_postData)
        $.ajax({
            type: "post",
            url: mdmPath + "/MDM0001/MDM000101",
            data: _postData,
            dataType: "json",
            success: function (re) {
                if(re.Status == 200){
                    _grid.trigger('reloadGrid');
                    $('.modal-overlay').addClass('hidden');
                }else{
                    myAlert(re.Message);
                }
            }
        });
    });
}

function setPwd(rowId){ //MDM000104
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        $('#m_Code').val(rowData.Code).attr('disabled', true);
        $('#m_Name').parent().parent().addClass('hidden');
        $('#m_Department').parent().parent().addClass('hidden');
        $('#m_Password').parent().parent().removeClass('hidden');
        $('.checkbox').addClass('hidden');
        $('.modal-overlay').removeClass('hidden');
        $('#submitBtn').unbind('click').bind('click', function(){
            var _postData = Object.create(postData);
            _postData.Content = {
                Code: $('#m_Code').val(),
                Password: $('#m_Password').val()
            };
            //console.log(_postData)
            $.ajax({
                type: "post",
                url: mdmPath + "/MDM0001/MDM000104",
                data: _postData,
                dataType: "json",
                success: function (re) {
                    if(re.Status == 200){
                        $('.modal-overlay').addClass('hidden');
                    }else{
                        myAlert(re.Message);
                    }
                }
            });
        });
    }else{
        myAlert('请选择您要设置的员工')
    }
}

function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata)!=='{}'){
        cellvalue = '<a href="javascript:0" opt-data="'+grid[0].id+'_edit"><i class="fa fa-pencil" title="编辑"></i></a>'+
            '&nbsp;<a href="javascript:0" opt-data="'+grid[0].id+'_set"><i class="fa fa-lock" title="设置密码"></i></a>'+
            '&nbsp;<a href="javascript:0" opt-data="'+grid[0].id+'_delete"><i class="fa fa-trash-o" title="删除"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function _gridInit(){   //MDM000102
    var _gridData = Object.create(gridData);
    _gridData.url = mdmPath + '/MDM0001/MDM000102';
    _gridData.colModel = [
        {label:'员工代码', name:'Code'},
        {label:'员工名称', name:'Name'},
        {label:'员工部门', name:'Department'},
        {label:'超级用户', name:'SuperUser'},
        {label:'操作', name:'Options', width:80, align:'center', formatter: alarmFormatter, sortable:false}
    ];
    _gridData.sortname = 'Code';
    var _postData = {
        Method: 'Q',
        Content:{
            SearchText: ''
        }
    };
    for(i in postData){
        _postData[i] = postData[i];
    }
    //console.log(postData)
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            postData : _postData,
        });
    };
    _gridData.loadComplete = function(xhr){
        xhr.Status == -999 && alert_QX($('body'), xhr.Message);
        gridSize();
        $('a[opt-data="jgTable_edit"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            editCode(rowId);
        });
        $('a[opt-data="jgTable_set"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            setPwd(rowId);
        });
        $('a[opt-data="jgTable_delete"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            deleteCode(rowId);
        });
    };
    gridInit(_gridData);
    gridSearch = _postData;
}

